import React from 'react';
import {
  Router as DefaultRouter,
  Route,
  Switch,
  StaticRouter,
} from 'react-router-dom';
import dynamic from 'umi/dynamic';
import renderRoutes from 'umi/lib/renderRoutes';
import history from '@@/history';
import RendererWrapper0 from 'C:/Users/s5698/Desktop/jyk-前台/jyk_front/src/pages/.umi/LocaleWrapper.jsx';
import _dvaDynamic from 'dva/dynamic';

const Router = require('dva/router').routerRedux.ConnectedRouter;

const routes = [
  {
    path: '/',
    component: __IS_BROWSER
      ? _dvaDynamic({
          component: () =>
            import(/* webpackChunkName: "layouts__BasicLayout" */ '../../layouts/BasicLayout'),
        })
      : require('../../layouts/BasicLayout').default,
    routes: [
      {
        path: '/',
        redirect: '/home',
        exact: true,
      },
      {
        path: '/CookiePage/:cookie',
        component: __IS_BROWSER
          ? _dvaDynamic({
              component: () =>
                import(/* webpackChunkName: "p__CookiePage" */ '../CookiePage'),
            })
          : require('../CookiePage').default,
        exact: true,
      },
      {
        path: '/home',
        name: '首页',
        component: __IS_BROWSER
          ? _dvaDynamic({
              component: () =>
                import(/* webpackChunkName: "p__Home" */ '../Home'),
            })
          : require('../Home').default,
        exact: true,
      },
      {
        path: '/dataAssets',
        name: '科研数据',
        component: __IS_BROWSER
          ? _dvaDynamic({
              component: () =>
                import(/* webpackChunkName: "p__DataAssets" */ '../DataAssets'),
            })
          : require('../DataAssets').default,
        exact: true,
      },
      {
        path: '/dataAssetsDetail/:id',
        name: '科研数据详情',
        component: __IS_BROWSER
          ? _dvaDynamic({
              component: () =>
                import(/* webpackChunkName: "p__DataAssets__detail" */ '../DataAssets/detail'),
            })
          : require('../DataAssets/detail').default,
        hideInMenu: true,
        exact: true,
      },
      {
        path: '/resourceSearch',
        name: '科研软件',
        component: __IS_BROWSER
          ? _dvaDynamic({
              component: () =>
                import(/* webpackChunkName: "p__ResourceSearch" */ '../ResourceSearch'),
            })
          : require('../ResourceSearch').default,
        exact: true,
      },
      {
        path: '/resourceSearchDetail/:id',
        name: '科研软件详情',
        component: __IS_BROWSER
          ? _dvaDynamic({
              component: () =>
                import(/* webpackChunkName: "p__ResourceSearch__detail" */ '../ResourceSearch/detail'),
            })
          : require('../ResourceSearch/detail').default,
        hideInMenu: true,
        exact: true,
      },
      {
        path: '/source',
        name: '科研数据检索',
        component: __IS_BROWSER
          ? _dvaDynamic({
              component: () =>
                import(/* webpackChunkName: "p__Source" */ '../Source'),
            })
          : require('../Source').default,
        exact: true,
      },
      {
        path: '/specialSubject',
        name: '专题',
        component: __IS_BROWSER
          ? _dvaDynamic({
              component: () =>
                import(/* webpackChunkName: "p__SpecialSubject" */ '../SpecialSubject'),
            })
          : require('../SpecialSubject').default,
        exact: true,
      },
      {
        path: '/specialSubjectDetail/:id',
        name: '专题详情',
        component: __IS_BROWSER
          ? _dvaDynamic({
              component: () =>
                import(/* webpackChunkName: "p__SpecialSubject__detail" */ '../SpecialSubject/detail'),
            })
          : require('../SpecialSubject/detail').default,
        hideInMenu: true,
        exact: true,
      },
      {
        path: '/standard',
        name: '标准与规范',
        component: __IS_BROWSER
          ? _dvaDynamic({
              component: () =>
                import(/* webpackChunkName: "p__Standard" */ '../Standard'),
            })
          : require('../Standard').default,
        exact: true,
      },
      {
        path: '/standardDetail/:id',
        name: '标准与规范详情',
        component: __IS_BROWSER
          ? _dvaDynamic({
              component: () =>
                import(/* webpackChunkName: "p__Standard__detail" */ '../Standard/detail'),
            })
          : require('../Standard/detail').default,
        hideInMenu: true,
        exact: true,
      },
      {
        path: '/statistics',
        name: '统计',
        component: __IS_BROWSER
          ? _dvaDynamic({
              component: () =>
                import(/* webpackChunkName: "p__Statistics" */ '../Statistics'),
            })
          : require('../Statistics').default,
        exact: true,
      },
      {
        path: '/helpdoc',
        name: '帮助文档',
        component: __IS_BROWSER
          ? _dvaDynamic({
              component: () =>
                import(/* webpackChunkName: "p__HelpDOC" */ '../HelpDOC'),
            })
          : require('../HelpDOC').default,
        hideInMenu: true,
        exact: true,
      },
      {
        path: '/helpdocdetail/:id',
        name: '帮助文档详情',
        component: __IS_BROWSER
          ? _dvaDynamic({
              component: () =>
                import(/* webpackChunkName: "p__HelpDOC__detail" */ '../HelpDOC/detail'),
            })
          : require('../HelpDOC/detail').default,
        hideInMenu: true,
        exact: true,
      },
      {
        component: () =>
          React.createElement(
            require('C:/Users/s5698/Desktop/jyk-前台/jyk_front/node_modules/umi-build-dev/lib/plugins/404/NotFound.js')
              .default,
            { pagesPath: 'src/pages', hasRoutesInConfig: true },
          ),
      },
    ],
  },
  {
    component: () =>
      React.createElement(
        require('C:/Users/s5698/Desktop/jyk-前台/jyk_front/node_modules/umi-build-dev/lib/plugins/404/NotFound.js')
          .default,
        { pagesPath: 'src/pages', hasRoutesInConfig: true },
      ),
  },
];
window.g_routes = routes;
const plugins = require('umi/_runtimePlugin');
plugins.applyForEach('patchRoutes', { initialValue: routes });

export { routes };

export default class RouterWrapper extends React.Component {
  unListen() {}

  constructor(props) {
    super(props);

    // route change handler
    function routeChangeHandler(location, action) {
      plugins.applyForEach('onRouteChange', {
        initialValue: {
          routes,
          location,
          action,
        },
      });
    }
    this.unListen = history.listen(routeChangeHandler);
    // dva 中 history.listen 会初始执行一次
    // 这里排除掉 dva 的场景，可以避免 onRouteChange 在启用 dva 后的初始加载时被多执行一次
    const isDva =
      history.listen
        .toString()
        .indexOf('callback(history.location, history.action)') > -1;
    if (!isDva) {
      routeChangeHandler(history.location);
    }
  }

  componentWillUnmount() {
    this.unListen();
  }

  render() {
    const props = this.props || {};
    return (
      <RendererWrapper0>
        <Router history={history}>{renderRoutes(routes, props)}</Router>
      </RendererWrapper0>
    );
  }
}
